<?php
function comboxPrs($db,$cbs_id=0,$cur_id=0,$prs_id = 0){
    
    
    $query = 'SELECT SQL_CACHE PRS_ID, PRS_NOM, DCP_NOM FROM PROFESSOR p, DISCIPLINA d '
            . 'WHERE p.DCP_ID = d.DCP_ID AND PRS_STA = 1 '
            . 'AND CBS_ID like "%'.$cbs_id.',%" AND CUR_ID like "%'.$cur_id.',%" ORDER BY PRS_NOM';
    $row = $db->fetchAll($query);
    
    //$html = '<select id="PRS_ID[]" name="PRS_ID[]" class="chosen-select">';
    //$html .= '<option value="">Professor</option>';
    $html = '';
    foreach($row as $r){           
        $selected = $r['PRS_ID'] == $prs_id ? 'selected=""':'';
        $html .= '<option '.$selected.' value="'.$r['PRS_ID'].'">'.$r['PRS_NOM'].' '.$r['DCP_NOM'].'</option>'; 
    }
    //$html .= '</select>';
    
    return $html;
}

?>

<div class="content-form">
    <h2><?php echo $this->titulo; ?></h2>
    <div id="content-form">
        <form id="form_pdr" action="" method="post" enctype="multipart/form-data">    
            <input type="hidden" id="QUH_ID" name="QUH_ID" value="<?php echo $this->data['QUH_ID']; ?>" />
            
            <div class="col_ln">
                <div class="ln"><strong>Curso</strong> 
                    <select id="CBS_ID" name="CBS_ID" required="">
                        <option value="">Selecione um Curso</option>
                    <?php foreach($this->cursos as $curso){ 
                        $selected = $curso['CBS_ID'] == $this->data['CBS_ID'] ? 'selected=""':'';
                    ?>
                        <option <?php echo $selected; ?>value="<?php echo $curso['CBS_ID'];?>"><?php echo $curso['CBS_NOM'];?></option>
                    <?php } ?> 
                    </select>
                </div>

                <div class="ln"><strong>Turma</strong> 
                    <span id="cnt_CUR_ID">
                        <select id="CUR_ID" name="CUR_ID" required="">
                            <option value="">Selecione uma Turma</option>
                            <?php
                            if($this->data['CBS_ID'] > 0){
                                $html = '';
                                
                                $query = 'SELECT SQL_CACHE c.CUR_ID, cb.CBS_NOM, cb.CBS_TPOTMP, n.NRT_NOM FROM CURSO c, CURSOBASE cb, NUMEROTURMA n WHERE c.CBS_ID = '.$this->data['CBS_ID'].' AND c.CBS_ID = cb.CBS_ID AND c.CUR_TRM = n.NRT_ID ORDER BY n.NRT_SEM ASC, n.NRT_NOM ASC, c.CUR_NOM ASC';
                                $row = $this->db->fetchAll($query);
                                
                                foreach($row as $r){ 
                                     $selected = $r['CUR_ID'] == $this->data['CUR_ID'] ? 'selected=""':'';
                                     $html .= '<option '.$selected.' value="'.$r['CUR_ID'].'">'.$r['NRT_NOM'].'</option>';
                                } 
                                echo $html;
                            }
                            ?>
                        </select>
                    </span>
                </div>

                <div class="ln"><strong>Descrição</strong> <input type="text" id="QUH_NOM" name="QUH_NOM" value="<?php echo $this->data['QUH_NOM']; ?>" required="" /></div>

                <div class="content-list">
                    <table id="tlist">
                        <thead>
                            <tr>
                                <th class="clear"></th>
                                <?php 
                                $arr_sem = Datetools::semana(null,3);

                                foreach($arr_sem as $key => $sem){ 
                                ?>
                                <th style="width: 100px;text-align: center;"><?php echo $sem; ?></th>
                                <?php } ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php 
                                $tr = '<tr><th>&nbsp;</th><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'; 
                            for($num = 1; $num <= 18; $num++){ 
                                
                                if($num == 7 || $num == 14){ 
                                    echo $tr;
                                }     
                            ?>
                            <tr>
                                <th style="text-align: right;"><?php echo $num;  ?></th>
                                <?php 
                                foreach($arr_sem as $key => $sem){ 
                                ?>
                                <td class="cbx_prs">
                                    <?php //echo comboxPrs($this->db); ?>
                                    <select name="PRS_ID[<?php echo $key; ?>][<?php echo $num; ?>]" class="chosen-select cbx_prs_sel">
                                        <option value="">Professor</option>
                                        <?php echo comboxPrs($this->db,$this->data['CBS_ID'],$this->data['CUR_ID'],$this->prs[$key][$num]); ?>
                                    </select>
                                </td>
                                <?php } ?>
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
                
                <div class="ln"><strong>Status</strong> <br>
                    <label><input type="checkbox" name="QUH_STA" id="QUH_STA" value="1" <?php if($this->data['QUH_STA'] == '' || $this->data['QUH_STA'] == 1) echo 'checked=""'; ?>> Ativo</label> &nbsp;
                </div>
                
                <div>
                    <input type="submit" name="submit" id="submitbutton" value="Salvar" class="btn-frm">

                    <a href="<?php echo $this->urlmodulo.'/'.$this->controller; ?>" class="btn-frm">voltar</a>
                </div>
                
            </div>
        </form>
    </div>
</div>